package com.lagou.rpc.consumer.proxy;

import com.alibaba.fastjson.JSON;
import com.lagou.rpc.common.RpcRequest;
import com.lagou.rpc.common.RpcResponse;
import com.lagou.rpc.consumer.client.RpcClient;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.data.Stat;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 客户端代理类-创建代理对象
 * 1.封装request请求对象
 * 2.创建RpcClient对象
 * 3.发送消息
 * 4.返回结果
 * @author: yx
 * @date: 2022/1/11 16:31
 */
public class RpcClientProxy {

    private static Map<String, RpcClient> clientMap = new ConcurrentHashMap<>();

    public static Object createProxy(Class serviceClass, CuratorFramework zkClient) {
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{serviceClass},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //1.封装request请求对象
                        RpcRequest rpcRequest = new RpcRequest();
                        rpcRequest.setRequestId(UUID.randomUUID().toString());
                        rpcRequest.setClassName(method.getDeclaringClass().getName());
                        rpcRequest.setMethodName(method.getName());
                        rpcRequest.setParameters(args);
                        rpcRequest.setParameterTypes(method.getParameterTypes());
                        //2。获取所有节点列表
                        String parentPath = "/zk";
                        List<String> nodeList = zkClient.getChildren().forPath(parentPath);
                        if (CollectionUtils.isEmpty(nodeList)) {
                            throw new RuntimeException("连接失败，获取不到节点列表");
                        }
                        List<Object> resultList = new ArrayList<>(nodeList.size());

                        for (String s : nodeList) {

                            //3.创建RpcClient对象
                            RpcClient rpcClient = getRpcClient(parentPath, s, zkClient);

                            if (rpcClient == null) continue;

                            try {
                                //4.发送消息
                                Object responseMsg = rpcClient.send(JSON.toJSONString(rpcRequest));
                                RpcResponse rpcResponse = JSON.parseObject(responseMsg.toString(), RpcResponse.class);
                                if (rpcResponse.getError() != null) {
                                    throw new RuntimeException(rpcResponse.getError());
                                }
                                //5.返回结果
                                Object result = rpcResponse.getResult();
                                Object resultJson = JSON.parseObject(result.toString(), method.getReturnType());
                                resultList.add(resultJson);
                            } catch (Exception e) {
                                System.out.println("出异常了: " + e.getMessage());
                                throw e;
                            }
                        }
                        return resultList.size() > 0 ? resultList.get(0) : null;
                    }
                });
    }

    /**
     * 获取客户端连接
     *
     * @param parentPath 父级路径
     * @param s          子节点路径
     * @param zkClient   zk客户端
     * @return
     * @throws Exception
     */
    private static RpcClient getRpcClient(String parentPath, String s, CuratorFramework zkClient) throws Exception {
        //节点路径
        String nodePath = parentPath + "/" + s;
        //判断节点的状态是否在线
        Stat stat = zkClient.checkExists().forPath(nodePath + "/stat");
        if (stat == null) {
            //不在线直接跳过
            return null;
        }
        //获取节点里的服务端数据
        byte[] bytes = zkClient.getData().forPath(nodePath);
        String data = new String(bytes, StandardCharsets.UTF_8);
        String[] split = data.split(":");
        String ip = split[0];
        String port = split[1];
        System.out.println(String.format("给ip: %s,port: %s发送消息", ip, port));
        RpcClient rpcClient = clientMap.get(nodePath);
        if (rpcClient == null) {
            rpcClient = new RpcClient(ip, Integer.valueOf(port));
            clientMap.put(nodePath, rpcClient);
        }
        //对状态节点进行监听 dataIsCompressed:是否进行数据压缩
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, nodePath, false);
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
                if (event.getData().getPath().equals(nodePath + "/stat") && event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
                    Thread.sleep(1000);
                    RpcClient rpcClient = new RpcClient(ip, Integer.parseInt(port));
                    clientMap.put(nodePath, rpcClient);
                    System.out.println(data + "重新建立连接成功");
                }
                if (event.getData().getPath().equals(nodePath + "/stat") && event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) {
                    RpcClient rpcClient1 = clientMap.get(nodePath);
                    if (rpcClient1 != null) {
                        rpcClient1.close();
                    }
                    clientMap.remove(nodePath);
                    System.out.println("删除节点: " + data + "监听成功");
                }
            }
        });
        return rpcClient;
    }
}
